package com.czn.controller.admin;

import com.czn.entity.Illness;
import com.czn.service.IIllnessService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 疾病管理控制器
 *
 * @author ChenZeNan
 * @create 2021-04-22 22:48
 */
@Controller
@RequestMapping("/admin/illness")
public class AdminIllnessController {

    @Autowired
    private IIllnessService illnessService;

    /**
     * 分页/模糊查询疾病
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/manage")
    private String illnessManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                 @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                 @RequestParam(required = false) String name,
                                 Model model) {
        PageQuery<Illness> page = illnessService.findIllnessList(pageNo, pageSize, name);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("name", name);
        model.addAttribute("path", "/admin/illness/manage");
        model.addAttribute("params", "");
        if (name != null) {
            model.addAttribute("params", "&name=" + name);
        }
        return "admin/illnessManage";
    }

    /**
     * 跳转到新增疾病页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("illness", new Illness());
        return "admin/illnessForm";
    }

    /**
     * 跳转到疾病修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("illness", illnessService.findIllnessById(id));
        return "admin/illnessForm";
    }

    /**
     * 新增疾病
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Illness illness) {
        ResponseResult result = illnessService.addIllness(illness);
        return result;
    }

    /**
     * 编辑疾病
     *
     * @param illness
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Illness illness) {
        ResponseResult result = illnessService.editIllness(illness);
        return result;
    }

    /**
     * 删除疾病
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = illnessService.deleteIllnessById(id);
        return result;
    }
}
